using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameModels;
using Microsoft.Xna.Framework.Storage;
using CrazyTankModel;

namespace CrazyTankTerrain
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            IsMouseVisible = true;
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            //graphics.IsFullScreen = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Load terrain
            float cellSize = 50;
            Texture2D heightMap = Content.Load<Texture2D>("map");
            Texture2D grass = Content.Load<Texture2D>("grass-texture");
            Texture2D snow = Content.Load<Texture2D>("snow-texture");
            Texture2D ground = Content.Load<Texture2D>("ground-texture");
            Texture2D three = Content.Load<Texture2D>("rock-road-texture");
            Texture2D detail = Content.Load<Texture2D>("Detail");
            Effect fx = Content.Load<Effect>("SimpleTerrainFX");
            Terrain terrain = new Terrain(this, heightMap, ground, grass, snow, detail, three, cellSize, 480, fx);
            this.Components.Add(terrain);

            //Load the tank
            Texture2D tankTexture = Content.Load<Texture2D>("engine_diff_tex");
            Effect tankEffect = Content.Load<Effect>("TankEffect");
            tankEffect.Parameters["TextureEnabled"].SetValue(true);
            tankEffect.Parameters["BasicTexture"].SetValue(tankTexture);
            Tank model = new Tank(this, "theTank", "l_front_wheel_geo", "r_front_wheel_geo", "l_back_wheel_geo", "r_back_wheel_geo", "turret_geo", "canon_geo", 50);
            model.Position = new Vector3(1000, 0, 1000);
            model.Scale = 0.3f;
            //model.SetEffect(tankEffect, "World", "View", "Projection");
            model.CanMove = true;
            model.ModelEye = new Vector3(0, 400, 0);

            //Add camera
            GameModels.Camera camera = new GameModels.Camera(this);
            camera.CameraType = CameraType.ThirdPerson;
            camera.Distance = 1000;
            camera.Height = 1000;
            camera.FocusedModel = model;

            //Load billboard
            BillBoard billBoard = new BillBoard(this);
            Texture2D tree = Content.Load<Texture2D>("tree");
            Effect billBoardEffect = Content.Load<Effect>("BillBoardEffect");
            //Random tree pos
            List<Vector3> lsTree = new List<Vector3>();
            List<float> lsHeight = new List<float>();
            Random rand = new Random(DateTime.Now.Millisecond);
            int w = (int)(heightMap.Bounds.Width * cellSize);
            int h = (int)(heightMap.Bounds.Height * cellSize);
            while (lsTree.Count < 100)
            {
                Vector3 vector = new Vector3();
                vector.Z = w / 2 - rand.Next((w));
                vector.X = h / 2 - rand.Next((h));
                lsTree.Add(vector);
                lsHeight.Add((float)rand.NextDouble() * tree.Height + tree.Height);
            }
            billBoard.CreateBillBoard(tree, "TreeTexture", lsTree.ToArray(), lsHeight.ToArray(), billBoardEffect, "World", "View", "Projection");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
